<template>
  <div class="app-container">
    <div>
      <el-button type="primary" size="medium" style="margin-bottom: 10px;" @click="getRes">生成奖学金结果</el-button>
      <el-button size="medium" style="margin-bottom: 10px;" @click="exportRes">导出奖学金结果</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="年份" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="学号" width="150">
        <template slot-scope="scope">
          {{ scope.row.uaccount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="学生姓名">
        <template slot-scope="scope">
          {{ scope.row.uname }}
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.className }}
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.majorName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学院" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.collegeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="奖学金名称" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.scholarshipName }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getApplyResultList, getExportRes, getResultList } from '@/api/apply'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getApplyResultList().then(response => {
        this.list = response.data;
        this.listLoading = false
      })
    },
    getRes() {
      this.listLoading = true;
      getResultList().then(res => {
        this.$message.success(res.data);
        this.listLoading = false;
        this.fetchData();
      })
    },
    exportRes() {
      this.listLoading = true;
      getExportRes().then(res => {
        let blob = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }) // 文件类型
        // let filename = window.decodeURI(res.headers['content-disposition'].split('=')[1])
        let url = window.URL.createObjectURL(blob);  // 创建下载链接
        let aLink = document.createElement("a");    // 赋值给a标签的href属性
        aLink.style.display = "none";
        aLink.href = url;
        aLink.setAttribute("download", "奖学金结果");
        document.body.appendChild(aLink);   // 将a标签挂载上去
        aLink.click();          // a标签click事件
        document.body.removeChild(aLink);  // 移除a标签
        window.URL.revokeObjectURL(url);   // 销毁下载链接
        this.listLoading = false
      })
    }
  }
}
</script>
